﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.TcCheckoutCheckin.CApTcCheckoutCheckinEnabler
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.TcCommands.TcCheckoutCheckin.PrivateImplementationDetails;
using EngineeringInternalExtension;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using UiInternalControls;


namespace DnProcessSimulateCommands.TcCommands.TcCheckoutCheckin;

public class CApTcCheckoutCheckinEnabler : ITxCommandEnabler
{
  private bool _onlyResourcesSelected;
  private bool _isSupportedPlatform;

  public CApTcCheckoutCheckinEnabler()
  {
    TxApplication.DocumentCollection.DocumentLoaded += new TxDocumentCollection_DocumentLoadedEventHandler(this.OnDocumentLoaded);
    TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(this.OnActiveDocumentUnloading);
  }

  internal bool OnlyResourcesSelected
  {
    get => this._onlyResourcesSelected;
    set => this._onlyResourcesSelected = value;
  }

  public bool IsValidSelection()
  {
    bool flag = false;
    int num = 0;
    foreach (ITxObject itxObject in (Collection<ITxObject>) TxApplication.ActiveSelection.GetItems())
    {
      flag = CApTcCIOUtil.IsValidOperationOrStudy(itxObject);
      if (flag)
        ++num;
      else
        flag = CApTcCIOUtil.IsValidResource(itxObject);
      if (!flag)
        break;
    }
    this.OnlyResourcesSelected = flag && num == 0;
    return flag;
  }

  private bool ShouldEnable()
  {
    return !TxTcStudyReservation.IsReadOnly() && this._isSupportedPlatform && this.IsValidSelection();
  }

  public bool Enable => this.ShouldEnable();

  private void OnDocumentLoaded(object sender, TxDocumentCollection_DocumentLoadedEventArgs args)
  {
    this._isSupportedPlatform = TxTcUtilities.IsTcOnlinePlatform;
    if (!this._isSupportedPlatform)
      return;
    this._isSupportedPlatform = !TxEngineeringDataInternal.GlobalGetStudyFormat().Equals((object) (TxStudyFormatEnum) 1);
  }

  private void OnActiveDocumentUnloading(object sender, TxDocument_UnloadingEventArgs args)
  {
    this._isSupportedPlatform = false;
  }
}
